<template>
    <div class="user-management">
      <div class="search-bar">
        <el-row gutter={20} align="middle">
          <el-col :span="4">
            <el-input placeholder="账号" v-model="query.account" />
          </el-col>
          <el-col :span="4">
            <el-input placeholder="用户名" v-model="query.username" />
          </el-col>
          <el-col :span="4">
            <el-select v-model="query.status" placeholder="状态" style="width: 100%;">
              <el-option label="正常" :value="1"></el-option>
              <el-option label="停用" :value="0"></el-option>
            </el-select>
          </el-col>
          <el-col :span="4">
            <el-select v-model="query.role" placeholder="角色" style="width: 100%;">
              <el-option label="管理员" :value="0"></el-option>
              <el-option label="用户" :value="1"></el-option>
            </el-select>
          </el-col>
          <el-col :span="4" class="button-col" style="text-align: right;">
            <el-button type="primary" @click="fetchUsers">查询</el-button>
            <el-button @click="resetFilters" style="margin-left: 10px;">重置</el-button>
          </el-col>
        </el-row>
      </div>
  
      <el-table :data="users" style="width: 100%" border>
        <el-table-column prop="account" label="账号" width="180"></el-table-column>
        <el-table-column prop="username" label="用户名" width="180"></el-table-column>
        <el-table-column prop="status" label="状态" width="120">
          <template slot-scope="scope">
            <span :style="{ color: scope.row.status === 1 ? 'green' : 'red' }">
              {{ scope.row.status === 1 ? '正常' : '停用' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="role" label="角色" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.role === 1 ? '管理员' : '用户' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="editUser(scope.row.id)" type="text" size="small">编辑</el-button>
            <el-button @click="deleteUser(scope.row.id)" type="text" size="small" style="color: red;">删除</el-button>
            <el-button @click="toggleStatus(scope.row.id, scope.row.status)" type="text" size="small">
              {{ scope.row.status === 1 ? '停用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <div class="pagination">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-size="pageSize"
          :total="totalUsers"
          layout="prev, pager, next"
        ></el-pagination>
      </div>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        query: {
          account: '',
          username: '',
          status: '',
          role: '',
        },
        users: [],
        page: 1,
        pageSize: 10,
        totalUsers: 0,
      };
    },
    methods: {
      fetchUsers() {
        const params = {
          account: this.query.account,
          username: this.query.username,
          status: this.query.status,
          role: this.query.role,
          page: this.page,
          pageSize: this.pageSize,
        };
  
        axios.get('/api/system/userList', { params }) // 修改为正确的后端接口
          .then(response => {
            this.users = response.data.data; // 假设返回的数据在data字段
            this.totalUsers = response.data.total; // 假设后端返回的总用户数为total
          })
          .catch(error => {
            console.error('查询用户时出错:', error);
          });
      },
      resetFilters() {
        this.query.account = '';
        this.query.username = '';
        this.query.status = '';
        this.query.role = '';
        this.fetchUsers(); // 重新获取用户列表
      },
      handleCurrentChange(newPage) {
        this.page = newPage;
        this.fetchUsers(); // 重新获取用户列表
      },
      editUser(userId) {
        // 编辑用户逻辑
        console.log(`编辑用户ID: ${userId}`);
      },
      deleteUser(userId) {
        // 删除用户逻辑
        console.log(`删除用户ID: ${userId}`);
      },
      toggleStatus(userId, currentStatus) {
        const newStatus = currentStatus === 1 ? 0 : 1;
        // 停用/启用用户逻辑
        console.log(`用户ID: ${userId} 将变为状态: ${newStatus}`);
      }
    },
    mounted() {
      this.fetchUsers(); // 组件加载后获取用户列表
    }
  };
  </script>
  
  <style scoped>
  .user-management {
    padding: 20px;
    background-color: #f0f2f5;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  }
  
  .search-bar {
    margin-bottom: 20px;
  }
  
  .button-col {
    text-align: right;
  }
  
  .pagination {
    text-align: right;
    margin-top: 20px;
  }
  </style>
  